package com.api.user.service.impl;

import com.api.base.config.auth.handler.ITaskService;
import com.api.base.dao.UserMapper;
import com.api.base.model.User;
import com.api.common.UtilFun;
import com.api.core.config.AuthUser;
import com.api.user.dao.TaskMapper;
import com.api.user.dao.UserInfoMapper;
import com.api.user.dao.UserTaskMapper;
import com.api.user.enums.IdentificationStatusEnums;
import com.api.user.model.PointsTransaction;
import com.api.user.model.Task;
import com.api.user.model.UserInfo;
import com.api.user.model.UserTask;
import com.api.user.service.PointsTransactionService;
import com.api.user.service.TaskService;
import com.api.core.service.AbstractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;

/**
 * Created by wanghuiwen on 2021/04/23.
 */
@Service
@Transactional
public class TaskServiceImpl extends AbstractService<Task> implements TaskService , ITaskService {
    @Resource
    private TaskMapper taskMapper;

    @Resource
    private UserTaskMapper userTaskMapper;

    @Resource
    private PointsTransactionService pointsTransactionService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public Result list(AuthUser authUser){
        List<Map<String, Object>> res = taskMapper.list(authUser.getId());
        return ResultGenerator.genSuccessResult(res);
    }

    @Override
    public void completeTask(String serial, Long userId,Integer complete) {
       Task task =  taskMapper.selectSerial(serial);
       User user = userMapper.selectByPrimaryKey(userId);
       UserInfo info = userInfoMapper.selectByPrimaryKey(userId);
       //普通用户并且 PRO MAX才能 完成任务
       if(user.getType()!=2 || !info.getVipLevel().equals(IdentificationStatusEnums.USER_LEVEL_PRO)){
           return;
       }

       //查询当天是否完成过
       UserTask userTask  = userTaskMapper.selectByUser(userId, UtilFun.DateToString(new Date(),UtilFun.YMD),task.getTaskId());

       if(userTask == null){
           userTask = new UserTask();
           userTask.setCreateTime(new Date().getTime());
           userTask.setState("1");
           userTask.setTaskId(task.getTaskId());
           userTask.setUserId(userId);
           userTask.setComplete(complete);
           userTask.setTarget(task.getTarget());
           userTaskMapper.insert(userTask);
       }else {
           userTask.setComplete(complete);
           userTaskMapper.updateByPrimaryKeySelective(userTask);
       }
    }

    @Override
    public Result receive(Long userTaskId, Long userId) {
        UserTask userTask = userTaskMapper.selectByPrimaryKey(userTaskId);
        if(userTask.getComplete() < userTask.getTarget()){
            return ResultGenerator.genFailResult();
        }
        if(userTask.getState().equals("2")){
            return ResultGenerator.genFailResult();
        }
        userTask.setState("2");
        userTaskMapper.updateByPrimaryKeySelective(userTask);
        Task task = taskMapper.selectByPrimaryKey(userTask.getTaskId());

        PointsTransaction pointsTransaction = new PointsTransaction();
        pointsTransaction.setToUserId(userId);
        pointsTransaction.setOrderId(task.getTaskId());
        pointsTransaction.setPointAmount(task.getReward());
        pointsTransaction.setTransactionTime(new Date().getTime());
        pointsTransaction.setTransactionType(2);
        pointsTransaction.setRemark("Task");
        pointsTransaction.setCreateId(userId);
        pointsTransactionService.addOrSub(pointsTransaction);

        return ResultGenerator.genSuccessResult();
    }

}
